﻿using System.Collections.Generic;
using System.Linq;

namespace Sustainalytics.LongRunningTaskManager.Interface
{
    /// <summary>
    /// Error occurred during the execution of a Long Running Task.
    /// </summary>
    public class LongRunningTaskError : IEqualityComparer<LongRunningTaskError>
    {
        /// <summary>
        /// Error code.
        /// </summary>
        public int ErrorCode { get; set; }

        /// <summary>
        /// Error arguments used to add additional information.
        /// </summary>
        public List<string> Arguments { get; set; }


        /// <summary>
        /// Ctor.
        /// </summary>
        public LongRunningTaskError()
        {
            this.Arguments = new List<string>();
        }

        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="errCode"> Error code. </param>
        public LongRunningTaskError(int errCode)
            : this()
        {
            this.ErrorCode = errCode;
        }


        public bool Equals(LongRunningTaskError x, LongRunningTaskError y)
        {
            if (x == null && y == null)
                return true;

            if (x == null || y == null)
                return false;

            var ok = x.ErrorCode == y.ErrorCode
                && ((x.Arguments == null && y.Arguments == null) || x.Arguments.SequenceEqual(y.Arguments));

            return ok;
        }

        public int GetHashCode(LongRunningTaskError obj)
        {
            return obj.ErrorCode.GetHashCode();
        }
    }
}
